package com.zxx.pattern.singlePattern;

/**
 * @author zxx on 2021/3/3
 */
public class SinglePattern {
    //饿汉模式
    private SinglePattern() {
    }

    private static SinglePattern singlePattern1 = new SinglePattern();

    public static SinglePattern getInstance1() {
        return singlePattern1;
    }

    //懒汉式
    private static SinglePattern singlePattern2;

    public static SinglePattern getInstance2() {
        if (singlePattern2 == null) {
            singlePattern2 = new SinglePattern();
        }
        return singlePattern2;
    }

    //懒汉式 （线程安全）--同步锁式
    private static SinglePattern singlePattern3;

    public static synchronized SinglePattern getInstance3() {
        if (singlePattern3 == null) {
            singlePattern3 = new SinglePattern();
        }
        return singlePattern3;
    }


    //双重检索式
    private static volatile SinglePattern singlePattern4;

    public static SinglePattern getInstance4() {
        if (singlePattern4 == null) {
            synchronized (SinglePattern.class) {
                if (singlePattern4 == null) {
                    singlePattern4 = new SinglePattern();
                }
            }
        }
        return singlePattern4;
    }


    //内部类式
    private static class Holder{
        private static SinglePattern INSTACE = new SinglePattern();
    }

    public static SinglePattern getInstance() {
        return Holder.INSTACE;
    }

}
